<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 9/28/12
 * Time: 10:29 AM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Products_MemoryList extends Page {
	/**
	 * @var Db_Products_MemoryList
	 */
	protected $tableObject;

	function __construct() {
		self::$TPL->appendJsFile('admin/memory_list');

		$this->idTableObject = get('id', 'int');
		$this->classTableObject = 'Db_Products_MemoryList';
		$this->actionsTableObject = array(
			'memoryListProductsAction'
		);
		parent::__construct();
	}

	public function defaultAction() {
		self::$TPL->setBodyTemplate('memory_list');
		return array(
			'type' => 'display'
		);
	}

	public function loadMemoryListDataAction() {
		$allColumns = array('nr', 'pr_name', 'ml_memory_identifier', 'ml_date', 'pg_name', 'mlp_name',);
		$searchColumns = array('pr_name', 'ml_memory_identifier', 'ml_date', 'pg_name', 'mlp_name');
		list($searchQuery, $orderBy, $limit) = Utils::getSqlQueriesForPagination($allColumns, $searchColumns, 'Db_Products_MemoryList', 'getTotalMemoryList');

		$memoryList = Db_Products_MemoryList::getMemoryList($limit, $searchQuery, $orderBy);

		self::$TPL->assign('memoryList', $memoryList);
		return array(
			'type' => 'encoded_json',
			'content' => self::$TPL->display('json_memory_list', true)
		);
	}

	public function memoryListProductsAction() {
		perm('PRODUCTS_MEMORY_LIST_PRODUCTS_VIEW', self::$PAGE['p_url']);
		if($this->tableObject->objectArray->id) {
			$memoryListId = $this->tableObject->objectArray->id;
		} else {
			return array(
				'type' => 'redirect',
				'query' => false
			);
		}
		$this->addBreadcrumb(t(T_PAGE_TITLE, 'products'), '?action=memoryListProducts&id='. $memoryListId, true);

		self::$TPL->setBodyTemplate('memory_list_products');
		return array(
			'type' => 'display'
		);
	}

	public function loadMemoryListProductsDataAction() {
		$allColumns = array('nr', 'mlp_name', 'p_name', 'te_name', 'mlp_date');
		$searchColumns = array('mlp_name', 'p_name', 'te_name', 'mlp_date');
		list($searchQuery, $orderBy, $limit) = Utils::getSqlQueriesForPagination($allColumns, $searchColumns, 'Db_Products_MemoryListProducts', 'getTotalMemoryListProducts');

		$memoryListProducts = Db_Products_MemoryListProducts::getMemoryListProducts($limit, $searchQuery, $orderBy);

		self::$TPL->assign('memoryListProducts', $memoryListProducts);
		return array(
			'type' => 'encoded_json',
			'content' => self::$TPL->display('json_memory_list_products', true)
		);
	}


}
